<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\model;

class CommShare extends \app\BaseModel
{
    protected $name = "massage_service_order_commission_share";
    public function dataAdd($data)
    {
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("status desc,id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function updateOne($id)
    {
        $user_id = $this->where(["id" => $id])->value("user_id");
        $res = $this->where(["user_id" => $user_id])->where("id", "<>", $id)->update(["status" => 0]);
        return $res;
    }
    public function addData($uniacid, $balance, $cash, $id, $type, $order_id, $share_id = 0, $cash_type = 0)
    {
        $insert = ["uniacid" => $uniacid, "comm_id" => $id, "share_balance" => $balance, "share_cash" => $cash, "type" => $type, "share_id" => $share_id, "order_id" => $order_id, "cash_type" => $cash_type];
        $res = $this->insert($insert);
        return $res;
    }
    public function getShareBalance($comm_id, $type)
    {
        $dis = ["comm_id" => $comm_id, "type" => $type];
        $balance = $this->where($dis)->value("share_balance");
        return !empty($balance) ? $balance : 0;
    }
    public function addPointData($id, $order, $type, $top_id)
    {
        $comm_model = new Commission();
        $cash = $comm_model->getTypeText($type);
        if(!empty($cash["point"]) && !empty($order["pay_point"]) && 0 < $order["pay_point"]) {
            $insert = ["uniacid" => $order["uniacid"], "comm_id" => $id, "share_balance" => $order["pay_point"], "share_cash" => $order[$cash["point"]], "order_id" => $order["id"], "cash_type" => 1, "share_id" => $top_id, "comm_type" => $type];
            $this->dataAdd($insert);
        }
        return true;
    }
    public function addPosterData($id, $order, $type, $comm_type, $top_id)
    {
        if(!empty($order["poster_cash"])) {
            if($type == 1) {
                $balance = $order["poster_coach_share"];
                $cash = $order["share_poster_coach"];
            } elseif($type == 2) {
                $balance = $order["poster_admin_share"];
                $cash = $order["share_poster_admin"];
            } else {
                $balance = $order["poster_company_share"];
                $cash = $order["share_poster_company"];
            }
            $insert = ["uniacid" => $order["uniacid"], "comm_id" => $id, "share_balance" => $balance, "share_cash" => $cash, "order_id" => $order["id"], "cash_type" => 2, "share_id" => $top_id, "type" => $type, "comm_type" => $comm_type];
            $res = $this->dataAdd($insert);
            return $res;
        }
        return true;
    }
    public function coachBalanceCash($id, $order, $top_id)
    {
        if(isset($order["comm_coach_balance"]) && 0 < $order["comm_coach_balance"]) {
            $insert = ["uniacid" => $order["uniacid"], "comm_id" => $id, "share_balance" => $order["comm_coach_balance"], "share_cash" => $order["comm_coach_cash"], "order_id" => $order["id"], "cash_type" => 3, "share_id" => $top_id, "type" => 1, "comm_type" => 3];
            $res = $this->dataAdd($insert);
            return $res;
        }
        return true;
    }
    public function coachSkillCash($id, $order, $top_id)
    {
        if(isset($order["skill_balance"]) && 0 < $order["skill_balance"] && !empty($order["coach_id"])) {
            $insert = ["uniacid" => $order["uniacid"], "comm_id" => $id, "share_balance" => $order["skill_balance"], "share_cash" => $order["skill_cash"], "order_id" => $order["id"], "cash_type" => 4, "share_id" => $top_id, "type" => 1, "comm_type" => 3];
            $res = $this->dataAdd($insert);
            return $res;
        }
        return true;
    }
    public function getOrderPointCash($order_id, $type = 1)
    {
        if($type == 1) {
            $cash = $this->where("order_id", "in", $order_id)->where(["cash_type" => 1])->sum("share_cash");
        } else {
            $cash = $this->where("order_id", "=", $order_id)->where(["cash_type" => 1])->sum("share_cash");
        }
        return round($cash, 2);
    }
    public function getCoachPointCash($coach_id)
    {
        $dis[] = ["b.top_id", "=", $coach_id];
        $dis[] = ["b.status", "=", 2];
        $dis[] = ["b.type", "in", [3, 8]];
        $dis[] = ["a.cash_type", "=", 1];
        $cash = $this->alias("a")->join("massage_service_order_commission b", "a.comm_id = b.id")->where($dis)->group("a.id")->sum("a.share_cash");
        return $cash;
    }
    public function coachShareCash($coach_id, $type, $order_id = 0)
    {
        $dis[] = ["a.share_id", "=", $coach_id];
        $dis[] = ["b.status", "=", 2];
        $dis[] = ["b.type", "=", $type];
        $dis[] = ["a.cash_type", "=", 0];
        $dis[] = ["a.type", "=", 1];
        if(!empty($order_id)) {
            $dis[] = ["b.order_id", "=", $order_id];
        }
        $cash = $this->alias("a")->join("massage_service_order_commission b", "a.comm_id = b.id")->where($dis)->group("a.id")->sum("a.share_cash");
        return round($cash, 2);
    }
    public function coachPosterCash($coach_id, $type)
    {
        $dis[] = ["a.share_id", "=", $coach_id];
        $dis[] = ["b.status", "=", 2];
        $dis[] = ["a.type", "=", 1];
        if($type == 3) {
            $dis[] = ["a.cash_type", "in", [3, 7, 24, 25]];
        } else {
            $dis[] = ["a.cash_type", "=", $type];
        }
        $cash = $this->alias("a")->join("massage_service_order_commission b", "a.comm_id = b.id")->where($dis)->group("a.id")->sum("a.share_cash");
        return $cash;
    }
    public function coachShareCashData($coach_id, $order_id = 0)
    {
        $arr = ["9" => "partner_share_cash", "10" => "channel_share_cash", "12" => "salesman_share_cash"];
        $order["total_share_cash"] = 0;
        foreach ($arr as $k => $v) {
            $order[$v] = $this->coachShareCash($coach_id, $k, $order_id);
            $order["total_share_cash"] += $order[$v];
        }
        $where = [];
        if(!empty($order_id)) {
            $where = ["order_id" => $order_id];
        }
        $order["point_cash"] = $this->where($where)->where(["cash_type" => 1, "comm_type" => 3])->sum("share_cash");
        $order["poster_cash"] = $this->where($where)->where(["cash_type" => 2, "type" => 1])->sum("share_cash");
        $order["coach_balance_cash"] = $this->where($where)->where(["cash_type" => 3, "type" => 1])->sum("share_cash");
        $order["total_share_cash"] = round($order["total_share_cash"] + $order["point_cash"] + $order["poster_cash"] + $order["coach_balance_cash"], 2);
        return $order;
    }
    public function shareCarPrice($order)
    {
        return $order;
    }
    public function financeShareData($where, $arr, $type = 1)
    {
        $list = ["2" => "poster_cash", "3" => "coach_balance_cash", "4" => "skill_cash", "1" => "point_cash"];
        $comm = new Commission();
        foreach ($list as $key => $value) {
            $dis = [["e.type", "=", 16], ["b.pay_type", ">", -1]];
            $dis1 = [["e.type", "<>", 16]];
            $arr[$value] = $comm->alias("a")->join("massage_service_order_list b", "a.order_id = b.id")->join("massage_service_order_commission e", "b.id = e.order_id")->join("massage_service_order_commission_share c", "c.comm_id = e.id")->where($where)->where(function ($query) use ($dis, $dis1){
                $query->whereOr([$dis, $dis1]);
            })->where(["c.cash_type" => $key, "e.status" => 2])->group("c.id")->column("c.share_cash");
            $arr[$value] = round(array_sum($arr[$value]), 2);
        }
        return $arr;
    }
    public function orderShareData($id)
    {
        $order_model = new Order();
        $comm = new Commission();
        $pay_type = $order_model->where(["id" => $id])->value("pay_type");
        if($pay_type == -1) {
            $cash = $comm->alias("a")->join("massage_service_order_commission_share c", "c.comm_id = a.id")->where(["a.order_id" => $id])->where(["c.cash_type" => 1])->where("a.status", "=", 2)->where("a.type", "in", [8, 13])->group("c.id")->sum("c.share_cash");
            return $cash;
        }
        $list = ["2" => "poster_cash", "point_cash"];
        $total_cash = 0;
        foreach ($list as $key => $value) {
            $cash = $comm->alias("a")->join("massage_service_order_commission_share c", "c.comm_id = a.id")->where(["a.order_id" => $id])->where(["c.cash_type" => $key])->where("a.status", ">", -1)->group("c.id")->sum("c.share_cash");
            $total_cash += $cash;
        }
        return $total_cash;
    }
    public function initShareDataStatus($coach_id)
    {
        $dis = ["a.status" => 2, "b.status" => -1];
        if(!empty($coach_id)) {
        }
        $data = $this->alias("a")->join("massage_service_order_commission b", "a.comm_id = b.id")->where(["a.status" => 2, "b.status" => -1])->field("a.id")->group("a.id")->limit(20000)->column("a.id");
        $this->where("id", "in", $data)->update(["status" => -1]);
        return true;
    }
    public function financeShareDataV2($where, $arr)
    {
        $list = ["2" => "poster_cash", "3" => "coach_balance_cash", "4" => "skill_cash", "1" => "point_cash"];
        $share = $this->alias("a")->join("massage_service_order_list b", "a.order_id = b.id")->where($where)->where(["a.status" => 2])->where("a.cash_type", "in", [1, 2, 3, 4])->where("a.comm_type", "not in", [8, 13])->group("a.cash_type")->field("round(sum(a.share_cash),2) as share_cash,a.cash_type")->select()->toArray();
        $sql = "";
        if(!empty($where)) {
            foreach ($where as $k => $v) {
                if(0 < $k) {
                    $and = " AND ";
                } else {
                    $and = "";
                }
                if(is_array($v[2])) {
                    $v[2] = implode(",", $v[2]);
                    if(!empty($v[2])) {
                        $v[2] = "(" . $v[2] . ")";
                    }
                }
                $sql .= $and . $v[0] . " " . $v[1] . " " . $v[2];
            }
        }
        foreach ($list as $key => $value) {
            $arr[$value] = 0;
            if(!empty($share)) {
                foreach ($share as $v) {
                    if($key == $v["cash_type"]) {
                        $arr[$value] = $v["share_cash"];
                    }
                }
            }
        }
        return $arr;
    }
    public function addCouponBearData($id, $order, $type, $comm_type, $top_id)
    {
        if(!empty($order["coupon_bear_type"]) && $order["coupon_bear_type"] == 2 && 0 < $order["coupon_id"]) {
            if($type == 1) {
                $balance = $order["coupon_bear"]["coupon_bear_coach"];
                $cash = $order["coupon_bear"]["coach_cash"];
            } elseif($type == 2) {
                $balance = $order["coupon_bear"]["coupon_bear_admin"];
                $cash = $order["coupon_bear"]["admin_cash"];
            } else {
                $balance = $order["coupon_bear"]["coupon_bear_company"];
                $cash = $order["coupon_bear"]["company_cash"];
            }
            if($balance <= 0) {
                return true;
            }
            $insert = ["uniacid" => $order["uniacid"], "comm_id" => $id, "share_balance" => $balance, "share_cash" => $cash, "order_id" => $order["id"], "cash_type" => 6, "share_id" => $top_id, "type" => $type, "comm_type" => $comm_type];
            $res = $this->dataAdd($insert);
            return $res;
        }
        return true;
    }
    public function addCarData($id, $order, $type = 1)
    {
        if(in_array($order["free_fare"], [1, 3])) {
            $comm_model = new Commission();
            $top_id = $comm_model->where(["id" => $id])->value("top_id");
            $comm_type = $comm_model->where(["id" => $id])->value("type");
            $insert = ["uniacid" => $order["uniacid"], "comm_id" => $id, "share_balance" => 0, "share_cash" => $order["car_price"], "order_id" => $order["id"], "cash_type" => 5, "share_id" => $top_id, "type" => $type, "comm_type" => $comm_type];
            $res = $this->dataAdd($insert);
            return $res;
        }
        return true;
    }
    public function addBalanceDiscountShare($id, $order, $top_id, $comm_type, $type)
    {
        if($order["pay_model"] == 4 && isset($order["coach_balancediscount_share_cash"])) {
            $share_model = new \app\balancediscount\model\OrderShare();
            if($type == 1) {
                $cash = isset($order["coach_balancediscount_share_cash"]) ? $order["coach_balancediscount_share_cash"] : 0;
                $balance = $share_model->where(["order_id" => $order["id"]])->value("coach_balance");
            } elseif($type == 2) {
                $cash = isset($order["admin_balancediscount_share_cash"]) ? $order["admin_balancediscount_share_cash"] : 0;
                $balance = $share_model->where(["order_id" => $order["id"]])->value("admin_balance");
            } else {
                $cash = isset($order["company_balancediscount_share_cash"]) ? $order["company_balancediscount_share_cash"] : 0;
                $balance = $share_model->where(["order_id" => $order["id"]])->value("company_balance");
            }
            if(0 < $balance) {
                $insert = ["uniacid" => $order["uniacid"], "comm_id" => $id, "share_balance" => $balance, "share_cash" => $cash, "order_id" => $order["id"], "cash_type" => 7, "share_id" => $top_id, "type" => $type, "comm_type" => $comm_type];
                $res = $this->dataAdd($insert);
                return $res;
            }
        }
        return true;
    }
}

?>